﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BiffenApplication.ServiceReference2;

namespace BiffenApplication
{
    public partial class NewMovie : Form
    {
        string result;
        RemoteBookingSystemClient cli = new RemoteBookingSystemClient();
        List<string> acto = new List<string>().ToList();

        string[] actorsL;

        public NewMovie()
        {
            InitializeComponent();
            textBox1.Text = "Titel";
            textBox2.Text = "Årstal (Valgfri)";
            plotLbl.Text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent ornare metus at dolor consequat tempor. Vestibulum ac elit turpis, a tempor ligula. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam varius urna a est pulvinar mollis. Duis commodo ante eu libero dapibus ac hendrerit nisl sodales. Donec a risus nec dolor fringilla pretium in sed enim. Mauris pulvinar pellentesque auctor. Praesent rhoncus augue at ligula iaculis vestibulum. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Phasellus imperdiet egestas turpis, sed gravida nisl posuere in. Fusce risus lacus, commodo sit amet molestie porta, ornare vel magna. Etiam elementum, mauris nec interdum lacinia, nulla arcu mollis ante, vel eleifend diam enim vel mauris. In dictum, mi et tincidunt gravida, diam tortor ultrices dui, id fringilla quam arcu vitae quam. Aenean ante urna, hendrerit sit amet fringilla eget, aliquet faucibus justo. Curabitur metus libero, tempus sit amet tincidunt quis, ornare in tellus.";






        }

        private string getMovieValue(string atr)
        {

            int countAtr = 0;
            countAtr = atr.Length;
            string start = result.Substring(result.IndexOf(atr) + countAtr + 3);
            string done = start.Substring(0, start.IndexOf("\""));
            return done;



        }

        public void getActors()
        {


            int countAtr = 0;
            countAtr = "Actors".Length;
            string actors = result.Substring(result.IndexOf("Actors") + countAtr + 3);
            string firstResult = actors.Substring(0, actors.IndexOf("Plot") - 3);
            string secondResult;

            while (firstResult.Contains(","))
            {
                string actor = firstResult.Substring(0, firstResult.IndexOf(","));

                actorsL = new string[]
                {
                    actor
                };
                secondResult = firstResult.Substring(firstResult.IndexOf(",") + 2);
                firstResult = secondResult;
            }
            actorsL = new string[]
                {
                    firstResult
                };




        }

        private void Search_Click(object sender, EventArgs e)
        {
            progressBar1.Value = 0;

            int year;
            if (textBox2.Text == "Årstal (Valgfri)")
                year = 0;
            else
                year = Convert.ToInt32(textBox2.Text);

            progressBar1.Value = 50;
            result = cli.getMovie(textBox1.Text, year);

            titleLbl.Text = getMovieValue("Title");
            yearLbl.Text = getMovieValue("Year");
            releasedLbl.Text = getMovieValue("Released");
            genreLbl.Text = getMovieValue("Genre");
            progressBar1.Value = 75;
            directorLbl.Text = getMovieValue("Director");
            writerLbl.Text = getMovieValue("Writer");
            getActors();
            listBox1.DataSource = actorsL;
            plotLbl.Text = getMovieValue("Plot");
            runtimeLbl.Text = getMovieValue("Runtime");
            ratingLbl.Text = getMovieValue("Rating") + " / 10";
            progressBar1.Value = 100;


            if (getMovieValue("Poster") != "N/A")
            {
                pictureBox1.ImageLocation = getMovieValue("Poster").Replace("SX320", "SX240");
            }
            else
            {
                pictureBox1.Image = global::BiffenApplication.Properties.Resources.noImage;
            }

        }

        private void textBox1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }

        private void textBox2_Click(object sender, EventArgs e)
        {
            textBox2.Text = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
            cli.newMovie(titleLbl.Text, directorLbl.Text, plotLbl.Text, genreLbl.Text, runtimeLbl.Text, pictureBox1.ImageLocation, actorsL);
                }
            catch(Exception ex)
            {
                plotLbl.Text = ex.ToString();
            }
            finally
            {
                       
                MessageBox.Show(this, "Filmen blev oprettet ! :D","Wheee", MessageBoxButtons.OK);
            }
            
        }
    }
}
